package me.magicall.test.net.download.diandian;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import me.magicall.net.download.Download;
import me.magicall.net.http.UrlConnectorConfig;
import me.magicall.util.kit.Kits;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Fetch {

	File saveDir;
	String baseUri;

	public void fetch() {
		final List<String> urls = Arrays.asList(Config.URLS);
		for (final String url : urls) {//每一个主页
			saveDir = new File("e:\\fetch\\" + Kits.STR.middle(url, "http://", false, ".", false));
			baseUri = Kits.STR.middle(url, "http://", true, "/", true);
			page(url);
		}
	}

	void page(final String url) {
		final Document doc = doc(url);
		if (doc == null) {
			return;
		}
		final Elements as = doc.select("div.main > div.group > div.items > a.item");
		for (final Element a : as) {//每一篇文章
			final String href = a.attr("href");
			article(href);
		}
	}

	void article(final String url) {
		final Document doc = doc(url);
		if (doc == null) {
			return;
		}
		final Elements as = doc.select("#post > div.photoShell > div.photo-div > div.large > a");
		for (final Element a : as) {//每一张照片
			final String href = a.attr("href");
			final File target = new File(saveDir, Kits.STR.subStringAfterLastSeq(href, "/"));

			final UrlConnectorConfig config = new UrlConnectorConfig();
			config.setUrl(href);
//			config.setMetaType(MetaType.JPG);
//			config.setConnectTimeout(connectTimeout);
//			config.setCookies(cookies);
//			config.setReadSteamEachSize(readSteamEachSize);
//			config.setReadTimeout(readTimeout);

			final Download download = new Download(config);
			download.setTarget(target);
			download.download();
		}
	}

	private Document doc(final String url) {
		try {
			return Jsoup.connect(url.startsWith(baseUri) ? url : baseUri + url).get();
		} catch (final Exception e) {
			e.printStackTrace();
			System.out.println("@@@@@@doc null:" + url);
			return null;
		}
	}

	public static void main(final String[] args) {
		new Fetch().fetch();
	}
}
